import React, { useState, useEffect } from 'react';
import { connect } from 'dva';
import EditTemplate from '@/components/EditTemplate';
import { timeFormat } from '@/utils/common';
import CapitalSource from '@/pages/components/capitalSource';
const BuildEdit = ({ location, dispatch, uuid, update }: any) => {
  const [detail, setDetail] = useState();
  const [approve, setapprove] = useState(null);
  const { id, readOnly } = location.query;

  const saveUrl = '/api/scheme/v1/create';
  useEffect(() => {
    if (id) {
      if (update === false) return;
      dispatch({
        type: '$request/post',
        payload: {
          url: '/api/scheme/v1/detail',
          data: {
            uuid: uuid || id,
            page: 0,
            size: 10,
          },
        },
      }).then(res => {
        console.log('储备项目申报详情：', res);
        res.examineRecordVO && approveInfo(res.examineRecordVO.uuid);
        setDetail({
          ...res.project,
          ...res,
          createTime: timeFormat(res.project.createTime),
        });
      });
      // ---------- 评审详情 -------
    }
  }, []);

  const approveInfo = id => {
    // 评审信息
    dispatch({
      type: '$request/post',
      payload: {
        url: '/api/examine/v1/record/detail',
        data: {
          uuid: id,
          page: 0,
          size: 10,
        },
      },
    }).then(res => {
      res.examineDate = timeFormat(new Date(res.examineDate), 'YYYY-MM-DD');
      res.examineOrgVOList = res.examineOrgVOList
        .map(item => item.name)
        .toString();
      res.examinePersonVOList = res.examinePersonVOList
        .map(item => item.name)
        .toString();
      setapprove(res);
    });
  };

  const projectChange = (uuid, data) => {
    if (update === false) return;
    dispatch({
      type: '$request/post',
      payload: {
        url: '/api/scheme/v1/project/query',
        data: { uuid },
      },
    }).then(list => {
      const res = list.find(item => item.uuid == uuid);
      res.createTime = timeFormat(new Date(), 'YYYY-MM-DD');
      setDetail(res);
    });
  };
  const filters = [
    {
      title: id ? '方案详情' : '上传方案',
      list: [
        {
          key: readOnly ? 'name' : 'projectUuid',
          type: id ? 'text' : 'select',
          label: '项目名称',
          rules: [{ required: true }],
          asyncopt: {
            url: '/api/scheme/v1/project/query',
          },
          prop: {
            onChange: (uuid, data) => {
              console.log('uuid', uuid);
              projectChange(uuid, data);
            },
          },
          span: 12,
        },
        {
          key: 'no',
          type: 'text',
          label: '项目ID',
          placeholder: '请输入姓名',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: ['ownerOrgUnit', 'name'],
          type: 'text',
          label: '业主单位',
          span: 12,
        },
        {
          key: 'createTime',
          type: 'text',
          label: '报送日期',
          span: 12,
        },
        {
          key: 'name',
          type: 'input',
          label: '方案名称',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'org',
          type: 'input',
          label: '方案设计单位',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'budget',
          type: 'input',
          label: '项目投资概算',
          prop: {
            addonAfter: '万元',
          },
          rules: [
            {
              required: true,
              pattern: new RegExp(/^-?\d+/, 'g'),
              message: '只能输入数字',
            },
          ],
          span: 12,
        },
        {
          key: 'fileList',
          type: 'upload',
          label: '方案电子材料',
          rules: [{ required: true }],
          span: 24,
        },
      ],
    },
  ];

  const approveFilters = [
    {
      title: '评审信息',
      list: [
        {
          key: 'examineOrgVOList',
          type: 'text',
          label: '评审机构',
          mode: 'multiple',
          span: 24,
        },
        {
          key: 'examinePersonVOList',
          type: 'text',
          label: '评审专家',
          mode: 'multiple',
          span: 24,
        },
        {
          key: 'examineDate',
          type: 'datepicker',
          label: '评审日期',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'examineAddress',
          type: 'input',
          label: '评审地点',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'suggestion',
          type: 'textarea',
          label: '评审意见',
          span: 24,
        },
        {
          key: 'changeAmount',
          type: 'input',
          label: '评审增减金额',
          placeholder: '增加金额输入正数，减少金额输入负数',
          span: 24,
          prop: {
            addonAfter: '万元',
          },
          rules: [
            {
              required: true,
              pattern: new RegExp(/^-?\d+/, 'g'),
              message: '只能输入数字',
            },
          ],
        },
        {
          key: 'fileList',
          type: 'upload',
          label: '评审电子材料',
          span: 24,
        },
      ],
    },
  ];
  const preproccess = data => {
    const fileUuidList = data.fileList
      ? data.fileList.map(item => item.uuid)
      : [];
    return {
      fileUuidList,
      ...data,
    };
  };
  return (
    <div>
      <EditTemplate
        filters={filters}
        initialValues={detail}
        updateUrl={saveUrl}
        preproccess={preproccess}
        readOnly={readOnly}
      />
      {approve && (
        <EditTemplate
          filters={approveFilters}
          initialValues={approve}
          updateUrl="/api/examine/v1/record/create"
          formLayout={{
            labelCol: { span: 6 },
          }}
          preproccess={preproccess}
          readOnly={true}
        />
      )}
    </div>
  );
};

export default connect((state: any) => state.auth)(BuildEdit);
